#include <iostream>

using namespace std;
void cout_ponit_val(int *a);

void handle_sigsegv(int sig);

int main()
{
    signal(SIGSEGV, handle_sigsegv);
    setlocale(LC_ALL, "zh_CN.UTF-8");
    cout << "hello,world" << endl;
    int a = 10;
    // 定义指针
    int *aP = &a;
    // 取指针的变量
    cout << "point var is " << *aP << endl;
    // 取指针的内存地址
    cout << "point memory adress is " << aP << endl;

    cout << "var a value is" << endl;
    cout << a << endl;
    cout << "ap value is " << endl;
    cout << aP << endl;
    cout << "*ap value is " << endl;
    cout << *aP << endl;
    a = 20;
    cout << "update var a value is 20,a value is " << endl;
    cout << a << endl;
    cout << "ap value is " << endl;
    cout << aP << endl;
    cout << "*ap value is " << endl;
    cout << *aP << endl;

    cout << "cout_ponit_val" << endl;
    cout_ponit_val(&a);

    int b = 1;
    int *bP1 = &b;
    int *bP2 = &b;
    cout << "bP1 value = " << *bP1 << endl;
    cout << "bP2 value = " << *bP2 << endl;
    *bP1 = 2;
    cout << "bP1 value = " << *bP1 << endl;
    cout << "bP2 value = " << *bP2 << endl;
    int b2 = 3;
    bP2 = &b2; // 改变指针的指向，而不是改变指针指向的内存的值
    cout << "bP1 value = " << *bP1 << endl;
    cout << "bP2 value = " << *bP2 << endl;
    *bP2 = b2;
    cout << "bP1 value = " << *bP1 << endl;
    cout << "bP2 value = " << *bP2 << endl;


    //空指针
    int *p1 = nullptr;
    p1 = NULL;
    p1 = 0;

    // 野指针
    int *c;
    // 抛异常    
    cout << *c << endl;
    
    
}

void cout_ponit_val(int *a)
{
    cout << *a << endl;
    return;
}

void handle_sigsegv(int sig) {
    std::cout << "throw err (SIGSEGV)" << std::endl;
    // 进行必要的清理
    std::exit(1);
}